﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using hzGis.model;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;

namespace hzGis.utils
{
    public class FeatureClassUtils
    {
        /// <summary>
        /// 打开shpfile
        /// </summary>
        /// <param name="shpFilePath"></param>
        /// <returns></returns>
        public static FeatureClassInfo openShpFile(string shpFilePath)
        {
            string pathName = System.IO.Path.GetDirectoryName(shpFilePath);
            string fileName = System.IO.Path.GetFileNameWithoutExtension(shpFilePath);
            //声明Shape文件工作空间工厂，并获取要素工作空间，加载要素类
            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pathName, 0);
            IFeatureWorkspace pFeatureWorkspace;
            pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName);
            FeatureClassInfo fci = new FeatureClassInfo();
            fci.fc = pFeatureClass;
            fci.wpf = pWorkspaceFactory;
            fci.wp = pWorkspace;
            return fci;
        }

        /// <summary>
        /// 打开mdb
        /// </summary>
        /// <param name="mdbPath"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static FeatureClassInfo OpenMDBWorkspace(string mdbPath, string tableName)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            IWorkspaceFactory pFactory = new AccessWorkspaceFactory() as IWorkspaceFactory;
            string filePath = mdbPath;
            IWorkspace pWorkspace = pFactory.OpenFromFile(filePath, 0);
            IFeatureWorkspace pFeatureWorkspace;
            pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = null;
            if (tableName != null)
            {
                pFeatureClass = pFeatureWorkspace.OpenFeatureClass(tableName);
            }
            FeatureClassInfo fci = new FeatureClassInfo();
            fci.fc = pFeatureClass;
            fci.wpf = pFactory;
            fci.wp = pWorkspace;
            return fci;
        }

        /// <summary>
        /// 打开gdb
        /// </summary>
        /// <param name="gdbPath"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static FeatureClassInfo OpenGDBWorkspace(string gdbPath, string tableName)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            IWorkspaceFactory pFactory = new FileGDBWorkspaceFactory() as IWorkspaceFactory;
            string filePath = gdbPath;
            IWorkspace pWorkspace = pFactory.OpenFromFile(filePath, 0);
            IFeatureWorkspace pFeatureWorkspace;
            pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = null;
            if (tableName != null)
            {
                pFeatureClass = pFeatureWorkspace.OpenFeatureClass(tableName);
            }
            FeatureClassInfo fci = new FeatureClassInfo();
            fci.fc = pFeatureClass;
            fci.wpf = pFactory;
            fci.wp = pWorkspace;
            return fci;
        }

        public static FeatureClassInfo openFeatureClass(string dbPath)
        {
            if (dbPath.IndexOf("shp") > -1)
            {
                return FeatureClassUtils.openShpFile(dbPath);
            }
            else if (dbPath.IndexOf("mdb") > -1)
            {

                if (dbPath.IndexOf('&') >= 0)
                {
                    string[] infos = dbPath.Split('&');
                    return FeatureClassUtils.OpenMDBWorkspace(infos[0], infos[1]);
                }
                else
                {
                    return FeatureClassUtils.OpenMDBWorkspace(dbPath, null);
                }
                
            }
            else if (dbPath.IndexOf("gdb") > -1)
            {
                if (dbPath.IndexOf('&') >= 0)
                {
                    string[] infos = dbPath.Split('&');
                    return FeatureClassUtils.OpenGDBWorkspace(infos[0], infos[1]);
                }
                else
                {
                    return FeatureClassUtils.OpenGDBWorkspace(dbPath, null);
                }
            }
            else
            {
                return null;
            }
        }
    }
}
